﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * CLR版本：4.0.30319.42000
 * 文件名：Kernel
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/15 13:24:47
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Gui;
using ImageK.IO;
using ImageK.Macros;
using ImageK.Plugin.Frame;
using ImageK.Text;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ImageK.Plugin
{
    /// <summary>
    /// Runs miscellaneous File and Window menu commands.
    /// </summary>
    public class Commands:PlugIn
    {
        public void run(string cmd)
        {
			if (cmd.Equals("new"))
            {
                if (IJ.altKeyDown())
                    IJ.runPlugIn("ij.plugin.HyperStackMaker", "");
                else
                    new NewImage();
            }
            else if (cmd.Equals("open"))
            {
                // if (Prefs.useJFileChooser && !IJ.macroRunning())
                //     new Opener().openMultiple();
                // else
                    new Opener().open();
            }
            else if (cmd.Equals("close"))
                close();
            else if (cmd.Equals("close-all"))
                closeAll();
            else if (cmd.Equals("save"))
                save();
            // else if (cmd.equals("revert"))
            //     revert();
            else if (cmd.Equals("undo"))
                undo();
            // else if (cmd.equals("ij"))
            // {
            //     ImageJ ij = IJ.getInstance();
            //     if (ij != null) ij.toFront();
            // }
            // else if (cmd.equals("tab"))
            //     WindowManager.putBehind();
            else if (cmd.Equals("quit"))
            {
                ImageK ij = IJ.GetInstance();
                if (ij != null) ij.quit();
            }
            // else if (cmd.equals("startup"))
            //     openStartupMacros();
		}

        // void revert()
        // {
        //     ImagePlus imp = WindowManager.getCurrentImage();
        //     if (imp!=null)
        //         imp.revert();
        //     else
        //         IJ.noImage();
        // }
        //
        void save()
        {
            ImagePlus imp = WindowManager.getCurrentImage();
            if (imp!=null)
            {
                if (imp.getStackSize()>1)
                {
                    imp.setIgnoreFlush(true);
                    new FileSaver(imp).save();
                    imp.setIgnoreFlush(false);
                }
                else
                    new FileSaver(imp).save();
            }
            else
                IJ.noImage();
        }

        void undo()
        {
            ImagePlus imp = WindowManager.getCurrentImage();
            if (imp != null)
                Undo.undo();
            else
                IJ.noImage();
        }

        void close()
        {
            ImagePlus imp = WindowManager.getCurrentImage();
            Form win = WindowManager.getActiveWindow();
            if (win==null || (Interpreter.isBatchMode() && win is ImageWindow))
			    closeImage(imp);
            else if (win is PlugInFrame && !"Commands".Equals(((PlugInFrame)win).Text))
			    ((PlugInFrame)win).Close();

            else if (win is PlugInDialog)
			    ((PlugInDialog)win).Close();

            else if (win is TextWindow)
			    ((TextWindow)win).Close();
            else
                closeImage(imp);
        }

        /** Closes all image windows, or returns 'false' if the user cancels the unsaved changes dialog box. */
        public static bool closeAll()
        {
            int[] list = WindowManager.getIDList();
            if (list!=null)
            {
                int imagesWithChanges = 0;
                for (int i = 0; i<list.Length; i++)
                {
                    ImagePlus imp = WindowManager.getImage(list[i]);
                    if (imp!=null && imp.changes) imagesWithChanges++;
                }
                if (imagesWithChanges>0 && !IJ.MacroRunning())
                {
                    GenericDialog gd = new GenericDialog("Close All");
                    String msg = null;
                    String pronoun = null;
                    if (imagesWithChanges==1)
                    {
                        msg = "There is one image";
                        pronoun = "It";
                    }
                    else
                    {
                        msg = "There are "+imagesWithChanges+" images";
                        pronoun = "They";
                    }
                    gd.addMessage(msg+" with unsaved changes. "+pronoun
                        +" will\nbe closed without being saved if you click \"OK\".");
                    gd.showDialog();
                    if (gd.wasCanceled())
                        return false;
                }
                Prefs.closingAll = true;
                for (int i = 0; i<list.Length; i++)
                {
                    ImagePlus imp = WindowManager.getImage(list[i]);
                    if (imp!=null)
                    {
                        imp.changes = false;
                        imp.close();
                    }
                }
                Prefs.closingAll = false;
            }
            return true;
        }

        void closeImage(ImagePlus imp)
        {
            if (imp==null)
            {
                IJ.noImage();
                return;
            }
            imp.close();
            if (Recorder.OnRecord && !IJ.isMacro())
            {
                //todo:
                // if (Recorder.scriptMode())
                //     Recorder.recordCall("imp.close();");
                // else
                //     Recorder.record("close");
                // Recorder.setCommand(null); // don't record run("Close")
            }
        }

        // Plugins>Macros>Open Startup Macros command
        // void openStartupMacros()
        // {
        //     Applet applet = IJ.getApplet();
        //     if (applet!=null)
        //         IJ.run("URL...", "url="+IJ.URL+"/applet/StartupMacros.txt");
        //     else
        //     {
        //         String path = IJ.getDirectory("macros")+"StartupMacros.txt";
        //         File f = new File(path);
        //         if (!f.exists())
        //         {
        //             path = IJ.getDirectory("macros")+"StartupMacros.ijm";
        //             f = new File(path);
        //         }
        //         if (!f.exists())
        //         {
        //             path = IJ.getDirectory("macros")+"StartupMacros.fiji.ijm";
        //             f = new File(path);
        //         }
        //         if (!f.exists())
        //             IJ.error("\"StartupMacros.txt\" not found in ImageJ/macros/");
        //         else
        //             IJ.open(path);
        //     }
        // }
    }
}
